import React, {Component} from 'react';
import {Image, View, Text} from 'react-native';
import PermissionDialog from '../PermissionDialog';
import {NoticeBar} from '@ant-design/react-native';
import {
  checkBgLocationPermission,
} from '../../../../src/utils/util';
import {IconImage} from '../../../utils';
import DeviceInfo from "react-native-device-info";

export default class PermissionNotice extends Component {
  constructor(props) {
    super(props);
    this.state = {
      hasBgLocationPermission: true,
    };
  }

  componentDidMount() {
    const osType = DeviceInfo.getSystemName();
    if (osType.toLocaleLowerCase() == 'android') {
      const osVersion = DeviceInfo.getSystemVersion()[0];
      if (Number(osVersion) >= 10) {
        logger('android 10及以上')
        this.getLocationPermission();
      }
    } else {
      this.getLocationPermission();
    }
    
  }

  getLocationPermission = async () => {
    let permissionResult = await checkBgLocationPermission();
    this.setState({hasBgLocationPermission: permissionResult});
  };

  componentWillUnmount = () => {
  };

  render() {
    const {hasBgLocationPermission} = this.state;
    return (
      <View>
        <View
          style={{position: 'absolute', bottom: 20, zIndex: 2, width: '100%'}}>
          {!hasBgLocationPermission && (
            <NoticeBar
              mode="closable"
              onPress={() => {
                this.permissionDialog.show();
              }}
              marqueeProps={{loop: true, style: {fontSize: 12}}}
              icon={undefined}>
              需要根据您的位置配送，请开启【始终允许获取位置】权限。
            </NoticeBar>
          )}
        </View>
        <PermissionDialog
          customContent={
            <View>
              <Text>请开启【始终允许获取位置】权限。</Text>
              <Image
                style={{width: 300, height: 200}}
                source={IconImage.bg_location_notice}
              />
            </View>
          }
          ref={r => (this.permissionDialog = r)}
          onDismissCallback={() =>
            this.getLocationPermission()
          }></PermissionDialog>
      </View>
    );
  }
}
